package com.salesmonitoring.view;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.KeyStroke;

import java.awt.Font;
import javax.swing.JButton;

import com.salesmonitoring.accounts.dao.AdminDaoImpl;
import com.salesmonitoring.accounts.dao.MarketingAssistantDaoImpl;
import com.salesmonitoring.accounts.view.AccountOfficerUI;
import com.salesmonitoring.accounts.view.AdminUI;
import com.salesmonitoring.accounts.view.MarketingAssistantUI;
import com.salesmonitoring.controller.LoginController;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

@SuppressWarnings("serial")
public class LoginScreen extends JFrame implements ActionListener {

	private JPanel loginScreenPanel;
	private JTextField unameTextField;
	private JPasswordField pwordField;
	private JLabel unameLabel, pwordLabel, errorLabel;
	private JButton loginButton;
	private LoginController lc;

	public LoginScreen() {
		initialize();
	}
	
	public void initialize() {
		
		setTitle("Bank of Makati | Log In");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 350, 230);

		loginScreenPanel = new JPanel();
		loginScreenPanel.setLayout(null);
		setContentPane(loginScreenPanel);
		
		errorLabel = new JLabel("");
		errorLabel.setFont(new Font("Helvetica", Font.PLAIN, 15));
		errorLabel.setBounds(49, 30, 200, 16);
		loginScreenPanel.add(errorLabel);
		
		unameLabel = new JLabel("Username");
		unameLabel.setFont(new Font("Helvetica", Font.PLAIN, 15));
		unameLabel.setBounds(49, 67, 77, 16);
		loginScreenPanel.add(unameLabel);
		
		unameTextField = new JTextField();
		unameTextField.setBounds(126, 60, 158, 28);
		loginScreenPanel.add(unameTextField);
		unameTextField.setColumns(10);
		
		pwordLabel = new JLabel("Password");
		pwordLabel.setFont(new Font("Helvetica", Font.PLAIN, 15));
		pwordLabel.setBounds(49, 95, 77, 16);
		loginScreenPanel.add(pwordLabel);
		
		pwordField = new JPasswordField();
		pwordField.setBounds(126, 88, 158, 28);
		loginScreenPanel.add(pwordField);
		
		loginButton = new JButton("Log In");
		loginButton.setFont(new Font("Helvetica", Font.PLAIN, 13));
		loginButton.setBounds(165, 122, 117, 29);
		loginScreenPanel.add(loginButton);
		
		
		loginButton.addActionListener(this);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		
		//this.lc = new LoginController(unameTextField.getText(), pwordField.getText());
		this.lc = LoginController.getInstance();
		
		lc.setUsername(unameTextField.getText());
		lc.setPassword(pwordField.getText());
		
		String role = lc.authenticate();
		
		if(role.equals("account officer")) {
			
			super.dispose();
			AccountOfficerUI aoUI = new AccountOfficerUI();
			aoUI.setCurrentUserName(lc.getName()+ "(Account Officer)");
			aoUI.setVisible(true);
			aoUI.setLocationRelativeTo(null);
		
		} else if(role.equals("admin")) {
			
			super.dispose();
			AdminUI adminUI = new AdminUI();
			adminUI.setCurrentUserName(lc.getName()+ "(Admininstrator)");
			adminUI.setVisible(true);
			adminUI.setLocationRelativeTo(null);
		
		} else if(role.equals("marketing assistant")) {
		
			super.dispose();
			MarketingAssistantUI marketingUI = new MarketingAssistantUI();
			marketingUI.setCurrentUserName(lc.getName()+ "(Marketing Assistant)");
			marketingUI.setVisible(true);
			marketingUI.setLocationRelativeTo(null);
		
		} else {
			errorLabel.setText("Invalid Login.");
			unameTextField.setText("");
			pwordField.setText("");
		}
		
		
	}
}
